package edu.ufl.android.poker;

import java.util.LinkedList;

import android.util.Log;

public class GameState {
	private LinkedList<Player> players;
	private int pot;
	private Player currPlayer;
	private Player dealer;
	private LinkedList<Card> communityCards;
	private int maxPlayers;
	private Deck myDeck;
	private int currBet;
	private boolean roundOver;
	
	public GameState()
	{
		pot = 0;
		maxPlayers = 8;
		players = new LinkedList<Player>();
		communityCards = new LinkedList<Card>();	
		myDeck = new Deck();
		roundOver = false;
	}
	public void addPlayer(Player p)
	{
		players.add(p);
	}
	public int addToPot(int toAdd)
	{
		pot += toAdd;
		return pot;
	}
	public void flop()
	{
		myDeck.drawFromDeck(); //burn a card
		for(int i=0; i<3; i++)
		{
			communityCards.add(myDeck.drawFromDeck());
		}
	}
	public int getPot()
	{
		return pot;
	}
	public void init()
	{
		addPlayer(new Player());
		addPlayer(new Player());
		addPlayer(new Player());
		addPlayer(new Player());
		
	}
	public void initRound()
	{
		dealer = players.get(0);
		currPlayer = dealer;
		for(int i=1; i<players.size(); i++)
		{
			players.get(i).receiveCard(myDeck.drawFromDeck());
		}
		dealer.receiveCard(myDeck.drawFromDeck());
		for(int i=1; i<players.size(); i++)
		{
			players.get(i).receiveCard(myDeck.drawFromDeck());
		}
		dealer.receiveCard(myDeck.drawFromDeck());
		logHoleCards(); 
		passTurnToNextPlayer();
	}
	public int getCurrBet()
	{
		return currBet;
	}
	public void logCommunityCards()
	{
		Log.d("Community Cards", " \nCommunity Cards \n------------------\n");
		for(int i=0; i<communityCards.size(); i++)
		{
			Log.d("Community Cards", communityCards.get(i).toString() + " \n");
		}	
	}
	public void logHoleCards()
	{
		Log.d("Hole Cards", " \n");
		for(int i=0; i<players.size(); i++)
		{
			Log.d("Hole Cards", "Player " + (i+1) + " \n----------------\n" + players.get(i).toStringHoleCards());
		}		
	}
	public void passTurnToNextPlayer()
	{
		int currPlayerIndex = players.indexOf(currPlayer);
		if(currPlayerIndex < players.size()-1)
		{
			currPlayer = players.get(currPlayerIndex+1);
		}
		
		else
		{
			currPlayer = players.get(0);
		}
		if(!currPlayer.playingThisRound())
		{
			passTurnToNextPlayer();
		}
		if(roundOver)
		{
			nextRound();
		}
	}
	public void nextRound()
	{
		
	}
	public void removePlayer(Player p)
	{
		players.remove(p);
	}
	
	public void river()
	{
		myDeck.drawFromDeck(); //burn a card
		communityCards.add(myDeck.drawFromDeck());
	}
	public void turn()
	{
		myDeck.drawFromDeck(); //burn a card
		communityCards.add(myDeck.drawFromDeck());
	}
	
	public boolean bet(int betAmount)
	{
		currPlayer.modifyStack(betAmount * (-1));
		pot += betAmount;
		return true;
	}
	public boolean check()
	{
		return true;
	}
	public boolean call()
	{
		currPlayer.modifyStack(currBet * (-1));
		pot += currBet;
		return true;
	}
	public boolean fold()
	{
		return true;
	}

}
